/**
 * CoffeeMilk (C) 2010 Amen Hamdan
 * 
 * See the NOTICE file distributed with this work for additional 
 * information regarding copyright ownership.  This file is licenses
 * to you under the Apache License, Version 2.0 (the "License"); you 
 * may not use this file except in compliance with the License.  You 
 * may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.    
 * 
 */
package ameen.rtm.lib.types;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Date;
import java.util.Vector;

public class RtmTask {
	public static final String NAME 			= "RtmTask.name";
	public static final String NEW_DUEDATE 		= "RtmTask.newduedate";
	public static final String NEW_LIST 		= "RtmTask.newslist";
	public static final String PRIORITY			= "RtmTask.priority";
	public static final String COMPLETED		= "RtmTask.completed";
	public static final String NEW_TAGS			= "RtmTask.newtags";
	public static final String NEW_NOTE			= "RtmTask.newnote";
	public static final String DELETE_NOTE		= "RtmTask.deletenote";
	public static final String NOTE_UPDATE		= "RtmTask.updatenote";
	
	private String fId, fName;
	private int fPriority = 0;
	private boolean fIsCompleted	= false;
	private Date fDueDate = new Date();
	private String fListId;
	private String fTaskSeriesId;
	private Vector<String> fTagsVector;
	private NotesVector fNotesVector;
	private String newDueDate; // required for updating the due date
	private RtmList fNewlist;    // required for moving the issue to another list
	private String newTagsString; // required for updating the tags of the issue (will be parsed by RTM
	private RtmTaskNote newTaskNote; // required for adding a new note 
    private final PropertyChangeSupport fPropertyChangeSupport = new PropertyChangeSupport(this);
    

    public RtmTask() { }
    
    public RtmTask(String id, String name) {
    	fId = id;
    	fName= name;
    }

	public void addPropertyChangeListener(PropertyChangeListener listener) {
		this.fPropertyChangeSupport.addPropertyChangeListener(listener);
	}
	
	public void removePropertyChangeListener(PropertyChangeListener listener) {
		this.fPropertyChangeSupport.removePropertyChangeListener(listener);
	} 
	
	public int getPriority() {
		return fPriority;
	}

	public boolean isCompleted() {
		return fIsCompleted;
	}

	public void setCompleted(boolean completed) {
		boolean oldValue = fIsCompleted;
		fIsCompleted = completed;
		fPropertyChangeSupport.firePropertyChange(COMPLETED, oldValue, fIsCompleted);
	}
	
	public Date getDueDate() {
		return fDueDate;
	}

	public String getId() {
		return fId;
	}

	public String getName() {
		return fName;
	}

	public void setPriority(String priority) {
		int oldValue = fPriority;
		fPriority = 0;
		if(priority.equals("1")) fPriority = 1;
		if(priority.equals("2")) fPriority = 2;
		if(priority.equals("3")) fPriority = 3;
		
		fPropertyChangeSupport.firePropertyChange(PRIORITY, oldValue, fPriority);
	}

	public void setDueDate(Date dueDate) {
		this.fDueDate = dueDate;
	}

	public void setId(String id) {
		this.fId = id;
	}

	public void setName(String name) {
		String oldValue = fName;
		this.fName = name;
		fPropertyChangeSupport.firePropertyChange(NAME, oldValue, fName);
	}

	public String getListId() {
		return fListId;
	}

	public void setListId(String listId) {
		this.fListId = listId;
	}

	public String getTaskSeriesId() {
		return fTaskSeriesId;
	}

	public void setTaskSeriesId(String taskSeriesId) {
		this.fTaskSeriesId = taskSeriesId;
	}

	public String getNewDueDate() {
		return newDueDate;
	}

	public void clearNewDueDate() {
		newDueDate = null;
	}
	
    public void addNote(String title, String text){
    	String[] parameters = { title, text };
		fPropertyChangeSupport.firePropertyChange(NEW_NOTE, null, parameters);
    }
 
    public void deleteNote(RtmTaskNote note) {
    	if(fNotesVector.remove(note)) {
    		fPropertyChangeSupport.firePropertyChange(DELETE_NOTE, note, null);
    	}
    }
    
    public RtmTaskNote getNewTaskNote() {
    	return newTaskNote;
    }
    
    public void clearnNewNote() {
    	newTaskNote = null;
    }
    
	public void clearNewTags() {
		newTagsString = null;
	}
	
	public void updateDueDate(String newDueDate) {
		this.newDueDate = newDueDate;
		fPropertyChangeSupport.firePropertyChange(NEW_DUEDATE, null, newDueDate);
	}	
	
	public void updateTags(String newTagsString) {
		this.newTagsString = newTagsString;
		fPropertyChangeSupport.firePropertyChange(NEW_TAGS, getTagsString(), newTagsString);
	}
	
	public void updateNote(RtmTaskNote taskNote, String newText) {
		taskNote.setText(newText);
	
		int endIndex = (newText.length() >=15)? 15: newText.length()-1;
		String title = newText.substring(0, endIndex);
		if(newText.length() > 15) {
			title += "..."; 
		}
		taskNote.setTitle(title);
		
		fPropertyChangeSupport.firePropertyChange(NOTE_UPDATE, null, taskNote);
	}
	
	public void cleanMoveTargetList() {
		fNewlist = null;
	}
	@Override
	public String toString() {
		return fName;
	}

	public RtmList getTargetMoveList() {
		return fNewlist;
	}

	public void setTargetMoveList(RtmList newlist) {
		this.fNewlist = newlist;
		fPropertyChangeSupport.firePropertyChange(NEW_LIST, null, fNewlist);
	}

	public Vector<String> getTags() {
		return fTagsVector;
	}

	public void setTags(Vector<String> fTags) {
		this.fTagsVector = fTags;
	}

	public NotesVector getNotes() {
		if(fNotesVector == null) {
			fNotesVector = new NotesVector();
		}
		return fNotesVector;
	}

	public void setNotes(NotesVector fNotes) {
		this.fNotesVector = fNotes;
	}
	
	public String getTagsString() {
		StringBuffer retStringBuffer = new StringBuffer();
		if(fTagsVector != null) {
			for(int i=0; i<fTagsVector.size(); i++) {
				retStringBuffer.append(fTagsVector.get(i));
				if(i<(fTagsVector.size()-1)) retStringBuffer.append(", ");
			}
		}
		return retStringBuffer.toString();
	}

	public String getNewTagsString() {
		return newTagsString;
	}
}
